package com.nondo.dean.auth.service.impl;

import com.nondo.dean.auth.domain.Company;
import com.nondo.dean.auth.domain.CompanyExample;
import com.nondo.dean.auth.mapper.CompanyMapper;
import com.nondo.dean.auth.service.CompanyService;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * project：dean-cloud
 *
 * @author linfeng @ nondo
 * @date 2018/4/23
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public Company findByCompanyId(Long companyId) {
        return companyMapper.selectByPrimaryKey(companyId);
    }

    @Override
    public List<Company> findByParentCompanyId(Long parentCompanyId) {

        CompanyExample example = new CompanyExample();
        CompanyExample.Criteria criteria = example.createCriteria();

        Company parentCompany = findByCompanyId(parentCompanyId);
        if (StringUtils.isNotBlank(parentCompany.getPath())) {
            criteria.andPathLike(parentCompany.getPath() + "%");
        }
        example.setOrderByClause("id asc");

        return companyMapper.selectByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByCompanyId(Long companyId) {
        companyMapper.deleteByPrimaryKey(companyId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAndUpdateCompany(Company company) {
        if (company.getId() == null) {

            company.setPath("/" + company.getName());
            companyMapper.insertSelective(company);
        } else {
            companyMapper.updateByPrimaryKeySelective(company);
        }
    }

    @Override
    public List<Company> findAll() {
        CompanyExample example = new CompanyExample();
        example.setOrderByClause("name asc");
        return companyMapper.selectByExample(example);
    }

    @Override
    public Page<Company> findCompanyPage(Query<Company> query) {
        CompanyExample example = new CompanyExample();
        CompanyExample.Criteria criteria = example.createCriteria();
        Company item = query.getData();
        if (item.getId() != null) {
            criteria.andIdEqualTo(item.getId());
        }
        if (item.getParentId() != null) {
            criteria.andParentIdEqualTo(item.getParentId());
        }
        if (StringUtils.isNotBlank(item.getName())) {
            criteria.andNameLike("'" + item.getName() + "%'");
        }
        example.setOrderByClause("id asc");
        List<Company> resultList = companyMapper.selectByExampleWithRowbounds(example, new RowBounds(query.getOffset(), query.getLimit()));
        long total = companyMapper.countByExample(example);

        Page<Company> resultPage = Page.of(query);
        resultPage.setTotalElements(total);
        resultPage.setContent(resultList);

        return resultPage;
    }

}
